﻿using System.Collections.Specialized;
using System.Diagnostics;
using System.Web.Routing;

namespace CodeContrib.Web.Routing
{
	public static class RoutingExtensions
	{
		[DebuggerStepThrough]
		public static RouteValueDictionary ToRouteValueDictionary(this NameValueCollection source)
		{
			var routeValues = new RouteValueDictionary();

			foreach (var key in source.AllKeys)
			{
				if (string.IsNullOrEmpty(key))
				{
					continue;
				}

				foreach (var value in source.GetValues(key))
				{
					routeValues.Add(key, value);
				}
			}

			return routeValues;
		}

		[DebuggerStepThrough]
		public static RouteValueDictionary ToRouteValueDictionary(this object source)
		{
			if (source is RouteValueDictionary)
			{
				return (RouteValueDictionary)source;
			}

			return new RouteValueDictionary(source.ToDictionary());
		}
	}
}